package com.whmskj.xjlhsc.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.whmskj.xjlhsc.controller.vo.EnergyVO;
import com.whmskj.xjlhsc.controller.vo.Result;
import com.whmskj.xjlhsc.service.EnergyService;
import com.whmskj.xjlhsc.service.ScHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

/**
 * 首页
 */
@SaIgnore
@RestController
@RequestMapping("/api")
public class ScHomeController {

    @Autowired
    private ScHomeService service;

    @Autowired
    private EnergyService energyService;



    //首页-产量
    @GetMapping("/home/getCl")
    public Result getCl(@RequestParam(defaultValue = "梳棉") String process) {
        Object result = service.getCl(process);
        return Result.success(result);
    }

    //首页-实时机台检测
    @GetMapping("/home/getJtjc")
    public Result getJtjc() {
        Object result = service.getJtjc();
        return Result.success(result);
    }

    //首页-员工信息
    @GetMapping("/home/getYgxx")
    public Result getYgxx() {
        Object result = service.getYgxx();
        return Result.success(result);
    }


    /**
     * 查询能耗数据
     * @param date 日期（格式：yyyy-MM-dd），可选，默认为当前日期
     * @param type 查询类型：today-今日, month-本月, year-本年
     * @return 能耗统计数据
     */
    @GetMapping("/energy/query")
    @SaIgnore
    public Result queryEnergyData(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date,
            @RequestParam String type) {

        // 验证查询类型
        if (!"today".equals(type) && !"month".equals(type) && !"year".equals(type)) {
            return Result.error("查询类型无效，可选值：today, month, year");
        }
        if (date == null) {
            date = LocalDate.now();
        }
        try {
            EnergyVO energyData = energyService.getEnergyData(date, type);
            return Result.success(energyData);
        } catch (Exception e) {
            return Result.error("查询能耗数据失败: " + e.getMessage());
        }
    }


}
